;(function(Ext,metaData){
	Ext.define('Ext.ux.AttrForm',{
		extend:'Ext.form.Panel',
		xtype:'attrform',
		bodyPadding: 15,
		scrollable:true,
		layout: 'anchor',
		defaults: {
		        anchor: '100%'
		 },
		defaultType: 'textfield',
		initComponent:function(){
			var me=this,
			initConfig=me.initialConfig,
			meteData=this.meteData=initConfig["metaData"];
			this.gridCmpId="grid"+metaData["id"],
			this.attrCmpId="attr"+metaData["id"],
			this.attrs=metaData["attrs"];
			this.idProperty=this.attrs["idProperty"];
			this.modelName=this.metaData["code"];
			this.fields=metaData["fields"];
			this.action=this.attrs["action"];
			me.initTbar();
			me.getFormItems();
			super.callParent();
			
		},
		
		
		initTbar:function(){
			var tbar=this.tbar=[{
				text:"新增",
				iconCls:"fa fa-add",
				handler:function(){
					
				}
			},{
				text:"编辑",
				iconCls:"fa fa-edit",
				handler:function(){
					
				}
			},{
				text:"删除",
				iconCls:"fa fa-delete",
				handler:function(){
					
				}
			},{
				text:"刷新",
				iconCls:'fa fa-refresh',
				handler:function(){
					
				}
			}];
			return tbar;
		},
		initButtons:function(){
			this.buttons=[{
		        text: '重置',
		        iconCls:"fa fa-refresh fa-fw",
		        handler: function() {
		            this.up('form').getForm().reset();
		        }
		    }, {
		        text: '提交',
		        iconCls:"fa fa-arrow-up",
		        formBind: true,
		        disabled: true,
		        handler: function() {
		            var form = this.up('form').getForm();
		            if (form.isValid()) {
		                form.submit({
		                    success: function(form, action) {
		                    	Ext.toast("提交成功",'提示', 't');
		                    	me.refreshSelectedNode();
		                    	
		                    },
		                    failure: function(form, action) {
		                    	Ext.toast(action.result.message,'提示', 't');
		                    	me.refreshSelectedNode();
		                    }
		                });
		            }
		        }
		    }];
		},
		getFormItems:function(){
			var fs=this.metaData["fields"],refEnumMap=this.metaData["refEnumMap"],items=this.items=[];
			Ext.iterate(fs,function(f,i){
				var item={},type=f["type"],attrs=f["attrs"],
				enums=attrs["enums"],picker=attrs["picker"];
				item["fieldLabel"]=f["text"];
				item["name"]=f["name"];
				if(attrs["defaultValue"]){
					item["defaultValue"]=attrs["defaultValue"];
				}
				if(enums){
					enums=enums.substring(enums.indexOf("$")+1);
					var enumdata=refEnumMap[enums];
					item["xtype"]='combo';
					item["queryMode"]='local';
					item["valueField"]=attrs["valueField"]||"code";
					item["displayField"]=attrs["displayField"]||"text";
					
					item["forceSelection"]=attrs["forceSelection"]||false;
					item["store"]={
						fields:["sid","name","code","text"],
						data:refEnumMap[enums]["list"]
					};
					item["triggerAction"]="all";
				}else if(picker){
					
					item["triggers"]={
							"trigger1":{
								handler:function(){
									var ctrl=this;
									Ext.create({
										valueHolder:ctrl,
										xtype:picker,
										modal:true
									}).show();
								}
							}
					};
				}else{
					switch(type){
					case 1:
						break;
					case 4:
					case -5:
						item["xtype"]='numberfield';
						break;
					case 91:
						item["xtype"]='datefield';
						item["format"]='Y-m-d';
						break;
					case 91:
						item["xtype"]='timefield';
						item["format"]='h:M:s';
						break;
					case 93:
						item["xtype"]='datefield';
						item["format"]='Y-m-d h:M:s';
						break;
					}
				}
				items.push(item);
			});
			return items;
		}
		
	});
})(Ext,metaData);

